﻿using System;
using System.Collections.Generic;
using System.Reactive.Subjects;
using System.Text;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Data;
using Avalonia.Input;
using Avalonia.Media;
using Avalonia.Media.Imaging;
using Avalonia.Styling;

namespace AvaloniaApplication1.Controls
{
    /// <summary>
    /// 图片按钮(分为选中和不选中两种状态)
    /// </summary>
    public class ImageButton : Button, IStyleable
    {
        Type IStyleable.StyleKey => typeof(Button);

        public static readonly StyledProperty<IBitmap> BackgroundImageProperty = AvaloniaProperty.Register<ImageButton, IBitmap>(nameof(BackgroundImage));
        /// <summary>
        /// 背景图片
        /// </summary>
        public IBitmap BackgroundImage
        {
            get { return GetValue(BackgroundImageProperty); }
            set { SetValue(BackgroundImageProperty, value); InitImage(); }
        }

        public static readonly StyledProperty<IBitmap> HoverImageProperty = AvaloniaProperty.Register<ImageButton, IBitmap>(nameof(HoverImage));
        /// <summary>
        /// 鼠标放上去的图片
        /// </summary>
        public IBitmap HoverImage
        {
            get { return GetValue(HoverImageProperty); }
            set { SetValue(HoverImageProperty, value); InitImage(); }
        }

        public static readonly StyledProperty<int> ImageWidthProperty = AvaloniaProperty.Register<ImageButton, int>(nameof(ImageWidth));
        /// <summary>
        /// 图片宽度
        /// </summary>
        public int ImageWidth
        {
            get { return GetValue(ImageWidthProperty); }
            set { SetValue(ImageWidthProperty, value); }
        }
        /// <summary>
        /// 图片高度
        /// </summary>
        public int ImageHeight { get; set; }

        private Image _image;
        protected Subject<IBitmap> _source;

        public ImageButton()
        {
            this.HorizontalContentAlignment = Avalonia.Layout.HorizontalAlignment.Stretch;
            this.VerticalContentAlignment = Avalonia.Layout.VerticalAlignment.Stretch;
            this.Padding = new Thickness();
            this.BorderThickness = new Thickness();
            this.Background = Brushes.Transparent;
            this.Cursor = new Cursor(StandardCursorType.Hand);

            InitContent();

            this.PointerEnter += ImageButton_PointerEnter;
            this.PointerLeave += ImageButton_PointerLeave;
            this.PointerPressed += ImageButton_PointerPressed;
        }

        private void ImageButton_PointerPressed(object sender, PointerPressedEventArgs e)
        {
            this.Background = Brushes.Transparent;
        }

        private void ImageButton_PointerLeave(object sender, PointerEventArgs e)
        {
            InitImage();
        }

        private void ImageButton_PointerEnter(object sender, PointerEventArgs e)
        {
            if (HoverImage != null)
            {
                _source.OnNext(HoverImage);
            }
        }

        public virtual void InitImage()
        {
            _source.OnNext(BackgroundImage);
        }

        private void InitContent()
        {
            _source = new Subject<IBitmap>();
            _source.OnNext(BackgroundImage);
            _image = new Image
            {
                Width = this.Width,
                Height = this.Height
            };
            _image.Bind(Image.SourceProperty, _source);
            Grid grid = new Grid
            {
                Background = Brushes.Transparent
            };
            grid.Children.Add(_image);
            this.Content = grid;
        }
    }

}
